// pages/write/write.js
// const wxAudio = wx.createInnerAudioContext()
// const wxAudio = wx.getBackgroundAudioManager()
// var  addr=[]
let wxAudio = null
Page({
    /**
     * 页面的初始数据
     */
    data: {
       lists:["好的","开始","再见","四季如春", "百花齐放"],
       Pathlists:[],
       isplay: false
    },
    getAudio (text) {
        wx.request({
            url: 'https://sis-ext.cn-east-3.myhuaweicloud.com/v1/fb1be9bec44e46418be8409b4eeeda0e/tts',
            method: 'post',
            header:{
                'Content-Type': 'application/json',
                'X-Auth-Token': "MIIN8AYJKoZIhvcNAQcCoIIN4TCCDd0CAQExDTALBglghkgBZQMEAgEwggwCBgkqhkiG9w0BBwGgggvzBIIL73sidG9rZW4iOnsiZXhwaXJlc19hdCI6IjIwMjQtMDktMjBUMTQ6MTQ6NTIuMDE5MDAwWiIsIm1ldGhvZHMiOlsiaHdfYWtfc2siXSwiY2F0YWxvZyI6W10sInJvbGVzIjpbeyJuYW1lIjoidGVfYWRtaW4iLCJpZCI6IjAifSx7Im5hbWUiOiJ0ZV9hZ2VuY3kiLCJpZCI6IjAifSx7Im5hbWUiOiJvcF9nYXRlZF9jc2JzX3JlcF9hY2NlbGVyYXRpb24iLCJpZCI6IjAifSx7Im5hbWUiOiJvcF9nYXRlZF9lY3NfZGlza0FjYyIsImlkIjoiMCJ9LHsibmFtZSI6Im9wX2dhdGVkX2Rzc19tb250aCIsImlkIjoiMCJ9LHsibmFtZSI6Im9wX2dhdGVkX29ic19kZWVwX2FyY2hpdmUiLCJpZCI6IjAifSx7Im5hbWUiOiJvcF9nYXRlZF9hX2NuLXNvdXRoLTRjIiwiaWQiOiIwIn0seyJuYW1lIjoib3BfZ2F0ZWRfZGVjX21vbnRoX3VzZXIiLCJpZCI6IjAifSx7Im5hbWUiOiJvcF9nYXRlZF9jYnJfc2VsbG91dCIsImlkIjoiMCJ9LHsibmFtZSI6Im9wX2dhdGVkX2Vjc19vbGRfcmVvdXJjZSIsImlkIjoiMCJ9LHsibmFtZSI6Im9wX2dhdGV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E5VDE0OjE0OjUyLjAxOTAwMFoiLCJ1c2VyIjp7ImRvbWFpbiI6eyJuYW1lIjoid25qaWUyMCIsImlkIjoiZTQyMTkxZjI1YjY4NDZlZWE2ZTRmMjk1MDVjMDhkZjIifSwibmFtZSI6InduamllMjAiLCJwYXNzd29yZF9leHBpcmVzX2F0IjoiIiwiaWQiOiIxOGJmMTJiY2UzYmY0ODAyYmIwNDg5YWZhMjFiOGVkYSJ9fX0xggHBMIIBvQIBATCBlzCBiTELMAkGA1UEBhMCQ04xEjAQBgNVBAgMCUd1YW5nRG9uZzERMA8GA1UEBwwIU2hlblpoZW4xLjAsBgNVBAoMJUh1YXdlaSBTb2Z0d2FyZSBUZWNobm9sb2dpZXMgQ28uLCBMdGQxDjAMBgNVBAsMBUNsb3VkMRMwEQYDVQQDDApjYS5pYW0ucGtpAgkA3LMrXRBhahAwCwYJYIZIAWUDBAIBMA0GCSqGSIb3DQEBAQUABIIBAGxcw8-k8aOHaM1zzhdRurqCqK8k25CMZ7eKSe9Wtt1zC86GBSCGUGUsHC2pVBN1TogRuGe0zr0Xd89AG1NAmbMJdVlAqPe4ZFLQH9YhHcRYRtIhk0ElvKgFANPdwIjAzw6YXVQIemjTnQEdmATAaCRd3LnOGBMczHjceEiUnMHI3IffZxGOAFxLCo1B4vksXQCs7NjeSVuWhMJ1IvmVUdOCW2nCOe-DD-z4FePXCogAPnWSet6WzbRNKAC1PKRlZtmr7LH-wYk6554B7r6OEMi1Ij8r-ydBSuG-liqXeh8YZAYdo8kBJp-nQzcDYMfFbEWHOcsXgIDeY-PXQH7zbno="
            },
            data: {
                'text': text,
                    "config": {
                            "audio_format": "mp3",
                            "sample_rate": "8000",
                            "property": "chinese_xiaowang_common",
                            "speed": 0,
                            "pitch": 0,
                            "volume": 50
                        }
            },
            responseType: 'text',
            success: res => {
                // const backgroundAudioManager = wx.getBackgroundAudioManager()
                // const wxAudio = wx.createInnerAudioContext()
                const audioPath = wx.env.USER_DATA_PATH + `/audio/${new Date().getTime()}.mp3`
                const fs = wx.getFileSystemManager();
                fs.writeFile({
                    filePath: audioPath,
                    data: res.data['result']['data'],
                    encoding: 'base64',
                    success(res){
                    // wxAudio.src = audioPath
                    console.log(audioPath)
                    // wxAudio.autoplay=true
                    }  
                })
                this.data.Pathlists.push(audioPath)
                // console.log(this.data.Pathlists)
                let currentIndex=0;
                this.play(this.data.Pathlists, currentIndex)
            }
        })
    },
    // 播放音频的递归函数：  
  play(arr, currentIndex){
    let InnerAudioContext = wx.createInnerAudioContext()
    InnerAudioContext.src = arr[currentIndex];
    InnerAudioContext.play();
    InnerAudioContext.onEnded((res) => {
        console.log('播放结束')//这个地方是上一个地址传出来的音频播放完以后才会运行这个函数，不能上一个还没有播完就换一个地址，这样会导致串音频，可以设置状态，让他播放完以后在进行传递新的数组。
        if(currentIndex < arr.length){//这个
            this.sleep(200)
            this.play(arr, currentIndex+1);
        }
    })
    },
    // 控制播放
    controlAudio () {
    let isPlay = this.data.isPlay
    this.setData({
        isPlay: !isPlay
    })
    isPlay ? wxAudio.pause() : wxAudio.play()
    },
    sleep: function(numberMillis) {
        var now = new Date();
        var exitTime = now.getTime() + numberMillis;
        while (true) {
            now = new Date();
            if (now.getTime() > exitTime)
                return;
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad:function () {
        for (var i in this.data.lists){
            this.getAudio(this.data.lists[i])
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {     
                   
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})